package com.damacheng009.future.alarm.crawler;

import java.util.ArrayList;
import java.util.List;

import org.nutz.json.Json;

import com.damacheng009.future.alarm.vo.KLineVo;
import com.damacheng009.future.alarm.vo.SinaStockKLineVo;

public class SinaStock60minCrawler extends BaseCrawler {

	@Override
	public List<KLineVo> getKLines(String code) {
		this.url = "https://quotes.sina.cn/cn/api/jsonp_v2.php/var%20_x=/CN_MarketDataService.getKLineData?symbol=" + code + "&scale=60&ma=no&datalen=1023";
		String str = wget(null);
		List<SinaStockKLineVo> list = Json.fromJsonAsList(SinaStockKLineVo.class, str);
		List<KLineVo> retList = new ArrayList<KLineVo>();
		
		if (list != null) {
			for (SinaStockKLineVo ssvo : list) {
				KLineVo vo = new KLineVo();
				vo.setC(ssvo.getClose());
				vo.setD(ssvo.getDay());
				vo.setH(ssvo.getHigh());
				vo.setL(ssvo.getLow());
				vo.setO(ssvo.getOpen());
				vo.setV(ssvo.getVolume());
				retList.add(vo);
			}
		}
		
		return retList;
	}

	@Override
	protected String clean(String originString) {
		String tmp = originString.replace("/*<script>location.href='//sina.com';</script>*/", "")
				.replace("var _x=(", "");
		return tmp = tmp.substring(0, tmp.length() - 2).trim();
	}

}
